const express = require("express")
const app = express()
const mysql = require("mysql")
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

const { load, cut } = require('@node-rs/jieba')
load()

app.listen(3000, () => {
  console.log("服务器开启3000端口...")
})

// 创建与数据库的连接
var connection = mysql.createConnection({
  host: '127.0.0.1',
  user: "mysql用户名",
  password: 'mysql密码',
  database: "scauInfo",
  port: '3306'
})

connection.connect((err) => {
  if (err) throw err
  console.log("连接成功")
})

// 全表模糊查询
app.post('/search', function (req, res) {
  let keyWords = req.body.keyWords
  let sortType = req.body.sortType
  let curPage = Number(req.body.curPage)
  if (keyWords == '%%') {
    let selectSQL = "select * from Info order by date desc, id"
    connection.query(selectSQL, function (err, rows, fields) {
      if (err) throw err
      if (rows.length == 0) {
        res.send({ status: 404, message: '暂无搜索结果' })
        return
      }
      let total = rows.length
      let pageSize = 20
      let data = rows.splice((curPage - 1) * pageSize, pageSize)
      res.send({ status: 200, message: 'get searchList', data: data, total: total })
    })
  }
  else {
    let tmp = cut(keyWords, false)
    splitKeyWords = tmp.slice(1, tmp.length - 1).join("|")
    let sql1 = "select * from userInput where words = '" + splitKeyWords + "'"
    connection.query(sql1, function (err, rows, fields) {
      if (err) throw err
      if (rows.length == 0) {
        sql2 = "insert into userInput(words,times) values('" + splitKeyWords + "',1)"
        connection.query(sql2, function (err, rows, fields) {
          if (err) throw err
        })
        return
      }
      let sql3 = "update userInput set times  = times + 1 where words = '" + splitKeyWords + "'"
      connection.query(sql3, function (err, rows, fields) {
        if (err) throw err
      })
    })
    // console.log('keywords', splitKeyWords)
    let selectSQL = ""
    if (sortType == 0) {
      selectSQL = "SELECT *,((IF( title REGEXP '" + splitKeyWords + "', 1, 0))+(IF( date REGEXP '" + splitKeyWords + "', 1, 0)) + (IF( `desc` REGEXP '" + splitKeyWords + "', 1, 0))) AS keyweight FROM Info WHERE CONCAT_WS(' ', title, date, `desc`) REGEXP '" + splitKeyWords + "' ORDER BY keyweight DESC"

    } else {
      selectSQL = "select * from Info where title REGEXP '" + splitKeyWords + "' or date REGEXP '" + splitKeyWords + "' or `desc` REGEXP '" + splitKeyWords + "' order by date desc, id"
    }
    console.log("selectSQL", selectSQL)
    connection.query(selectSQL, function (err, rows, fields) {
      if (err) throw err
      if (rows.length == 0) {
        res.send({ status: 404, message: '暂无搜索结果' })
        return
      }
      let total = rows.length
      let pageSize = 20
      let data = rows.splice((curPage - 1) * pageSize, pageSize)
      res.send({ status: 200, message: 'get searchList', data: data, total: total, splitKeyWords: splitKeyWords })
    })
  }
})

app.get('/getSuggestWords', function (req, res) {
  let selectSQL1 = "select word from highFreWords"
  connection.query(selectSQL1, function (err, rows, fields) {
    if (err) throw err
    let data1 = rows.map((item) => {
      return {
        value: item['word']
      }
    })
    let selectSQL2 = "select words from userInput where times > 5"
    connection.query(selectSQL2, function (err, rows, fields) {
      if (err) throw err
      let data2 = rows.map((item) => {
        return {
          value: item['words'].replace('|', ' ')
        }
      })
      data1 = data1.concat(data2)
      res.send({ status: 200, message: 'get suggestWords', data: data1 })
    })
  })
})